package com.example.book_shop_java.controller;

import com.alibaba.fastjson.JSON;
import com.example.book_shop_java.bean.Goods;
import com.example.book_shop_java.dao.GoodsDao;
import com.example.book_shop_java.util.UploadUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class GoodsController {

    @Resource
    GoodsDao goodsDao;

    @RequestMapping("/uploadGoodImg")
    public String uploadGoodImg(@RequestParam("file") MultipartFile multipartFile){
        String goodImg = UploadUtil.upload(multipartFile);
        HashMap<String ,Object> res = new HashMap<>();
        res.put("Url",goodImg);
        String goodImgUrl = JSON.toJSONString(res);
        return goodImgUrl;
    }

    @RequestMapping("/addGoods")
    public String addGoods(@RequestParam("goodName") String goodName,
                           @RequestParam("goodPrice") String goodPrice,
                           @RequestParam("goodImg") String goodImg,
                           @RequestParam("goodInventory") String goodInventory,
                           @RequestParam("goodIntro") String goodIntro,
                           @RequestParam("goodType") String goodType){
        int i = goodsDao.addGoods(goodName,goodPrice,goodImg,goodInventory,goodIntro,goodType);
        String message = "error";
        int code = 413;
        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String,Object> res = new HashMap<>();
        res.put("code",code);
        res.put("message",message);
        String addGoodsMessage = JSON.toJSONString(res);
        return addGoodsMessage;
    }

    @RequestMapping("/getAllGoods")
    public String getAllGoods(@RequestParam("goodName") String goodName){
        List<Goods> goods = goodsDao.getAllGoods("%"+goodName+"%");
        String message = "error";
        int code = 413;
        if(goods.size() > 0){
            message = "success";
            code = 200;
        }
        HashMap<String,Object> res = new HashMap<>();
        res.put("code",code);
        res.put("message",message);
        res.put("goods",goods);
        String getGoodsMessage = JSON.toJSONString(res);
        return getGoodsMessage;
    }

    @RequestMapping("/deleteGoods")
    public String deleteGoods(int good_id){
        int i = goodsDao.deleteGoods(good_id);
        String message = "error";
        int code = 413;
        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String,Object> res = new HashMap<>();
        res.put("code",code);
        res.put("message",message);
        String deleteGoodsMessage = JSON.toJSONString(res);
        return deleteGoodsMessage;
    }

    @RequestMapping("/editGoods")
    public String editGoods(int good_id,
                            @RequestParam("goodName") String goodName,
                            @RequestParam("goodPrice") String goodPrice,
                            @RequestParam("goodIntro") String goodIntro,
                            @RequestParam("goodType") String goodType){
        int i = goodsDao.editGoods(good_id,goodName,goodPrice,goodIntro,goodType);
        String message = "error";
        int code = 413;
        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String,Object> res = new HashMap<>();
        res.put("code",code);
        res.put("message",message);
        String editGoodsMessage = JSON.toJSONString(res);
        return editGoodsMessage;
    }

    @RequestMapping("/getGoodType")
    public String getGoodType(@RequestParam("goodType") String goodType){
        List<Goods> goods = goodsDao.getGoodType(goodType);
        String message = "error";
        int code = 413;
        if(goods.size() > 0){
            message = "success";
            code = 200;
        }
        HashMap<String,Object> res = new HashMap<>();
        res.put("code",code);
        res.put("message",message);
        res.put("goods",goods);
        String getGoodsMessage = JSON.toJSONString(res);
        return getGoodsMessage;
    }

    @RequestMapping("/getOneGood")
    public String getOneGood(int good_id){
        System.out.println(good_id);
        List<Goods> good = goodsDao.getOneGood(good_id);
        String message = "error";
        int code = 413;
        if(good.size() > 0){
            message = "success";
            code = 200;
        }
        HashMap<String,Object> res = new HashMap<>();
        res.put("code",code);
        res.put("message",message);
        res.put("good",good);
        String getGoodMessage = JSON.toJSONString(res);
        return getGoodMessage;
    }

    @RequestMapping("/addCar")
    public String addOrder(@RequestParam("goodImg") String goodImg,
                           @RequestParam("goodName") String goodName,
                           @RequestParam("goodPrice") String goodPrice,
                           @RequestParam("goodNum") String goodNum,
                           @RequestParam("wx_userid") int wx_userid){
        int a = goodsDao.countGoodId(goodName,wx_userid);
        if(a > 0){
            String Message = "购物车已有该商品!";
            return Message;
        }
        System.out.println(goodImg+"+"+goodName+"+"+goodPrice+"+"+goodNum+"+"+wx_userid);
        int i = goodsDao.addCar(goodName,goodImg,goodPrice,goodNum,wx_userid);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }
        res.put("message",message);
        res.put("code",code);
        String addCarMessage = JSON.toJSONString(res);
        return addCarMessage;
    }
}
